package com.lovo.sheyingback.controller;

import com.lovo.sheyingback.bean.SystemDict;
import com.lovo.sheyingback.bean.Type;
import com.lovo.sheyingback.bean.form.TypeForm;
import com.lovo.sheyingback.result.ResponseResult;
import com.lovo.sheyingback.result.UserValid;
import com.lovo.sheyingback.service.TypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/types")
@ResponseResult
@Tag(name = "类别管理TypeController")
@SecurityRequirement(name = "bearer-key")
//@UserValid
public class TypeController {
    @Autowired
    private TypeService typeService;
    @GetMapping
    @Operation(summary = "查询所有类别")
    public Object listType(TypeForm typeForm){
        return typeService.listType(typeForm.getCurrent(),typeForm.getPageSize());
    }
    @GetMapping("/all")
    @Operation(summary = "查询所有类别（不带分页）")
    public Object listAllType(){
        return typeService.listAllType();
    }
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询类别")
    public Object getById(@PathVariable Integer id){
        return typeService.getById(id);
    }
    @PostMapping
    @Operation(summary = "增加类别")
    public Object addType(@RequestBody TypeForm typeForm){
        Type type = new Type();
        BeanUtils.copyProperties(typeForm,type);
        type.setCreateDate(LocalDateTime.now());
        type.setUpdateDate(LocalDateTime.now());
        return typeService.insert(type);
    }
    @PutMapping("/{id}")
    @Operation(summary = "修改类别")
    public Object updateType(@PathVariable Integer id,@RequestBody TypeForm typeForm){
        Type type = new Type();
        BeanUtils.copyProperties(typeForm,type);
        type.setId(id);
        if(typeForm.getStatusId() != null){
            type.setStatus(new SystemDict(typeForm.getStatusId()));
        }
        return typeService.update(type);
    }

    @PutMapping("/index/{id}")
    @Operation(summary = "修改类别上移或下移")
    public Object updateTypeIndex(@PathVariable Integer id,@RequestBody TypeForm typeForm){
        return typeService.updateIndex(id,typeForm.getVal());
    }



}
